//
//  BGNewFeaturesViewController.m
//  BluetoothCameraHead
//
//  Created by wang on 16/11/1.
//  Copyright © 2016年 BG. All rights reserved.
//

#import "BGNewFeaturesViewController.h"

static const NSInteger kImageCount = 4; // 新特性图片个数

@interface BGNewFeaturesViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *continueButton;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger startPageIndex;
@property (nonatomic, assign) NSInteger stopPageIndex;
@end

@implementation BGNewFeaturesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    // 显示新特性图片
    [self showNewFeaturesImages];
}


/**显示新特性图片*/
- (void)showNewFeaturesImages {
    for (NSInteger i = 0; i < kImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        NSString *imageNameSuffix = @"";
        if (iPhone5) {
            imageNameSuffix = @"_568h";
        }else if (iPhone6) {
            imageNameSuffix = @"_667h";
        }else if (iPhone6plus) {
            imageNameSuffix = @"_736h";
        }
        
        NSString *imageName = [NSString stringWithFormat:@"guide%zd%@.jpg", i + 1, imageNameSuffix];
        NSString *fileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName];
        [imageView setImage:[[UIImage alloc]initWithContentsOfFile:fileName]];
        [self.scrollView addSubview:imageView];
        
        if (i == kImageCount - 1) {
            CGFloat continueBtnW = self.continueButton.currentImage.size.width; // 按钮宽度
            CGFloat continueBtnH = self.continueButton.currentImage.size.height; // 按钮高度
            CGFloat bottomPadding = 0.18 * SCREEN_HEIGHT;// 底边距离
            CGFloat continueBtnX = CGRectGetMinX(imageView.frame) + (SCREEN_WIDTH - continueBtnW) * 0.5;
            CGFloat continueBtnY = SCREEN_HEIGHT - continueBtnH - bottomPadding - 10;
            self.continueButton.frame = CGRectMake(continueBtnX, continueBtnY, continueBtnW, continueBtnH);
            [self.scrollView addSubview:self.continueButton];
        }else{
            [self gotoMainPage];
        }
    }
    self.pageControl.numberOfPages = kImageCount;
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * kImageCount, 0);
}

- (void)gotoMainPage {
    // 发出切换根视图的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:BGSwitchRootViewControllerNotification object:@{ @"isUserFirst" : @YES }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger currentPage = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % (kImageCount + 1);
    self.pageControl.currentPage = currentPage;
}

#pragma mark - lazy loading
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIButton *)continueButton {
    if (!_continueButton) {
        _continueButton = [[UIButton alloc]init];
        [_continueButton setImage:[UIImage imageNamed:@"guide_btn"] forState:UIControlStateNormal];
        [_continueButton setImage:[UIImage imageNamed:@"guide_btn_pressed"] forState:UIControlStateHighlighted];
        [_continueButton addTarget:self action:@selector(gotoMainPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _continueButton;
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        CGFloat pageControlH = 20;
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)- 2 * pageControlH, CGRectGetWidth([UIScreen mainScreen].bounds), pageControlH)];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = BGColorRGBA(239, 252, 238,0.8);;
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}



@end
